package dm.ast;

import test.envset.GlobalState;
import dm.ast.DmConstant.*;
/**
 * dm的Like表达式
 * 1、使用LIKE谓词的查询
 * 2、使用.ROW进行LIKE谓词的查询
 */
public class DmLikeExpression extends DmBoolExpression{
    boolean rowFlag = false;  //使用.ROW进行LIKE谓词的查询
    DmTableSource tab = null;

    boolean escapeFlag = false;  //使用转移字符标志
    DmConstant escapeChar = null;  //转义字符，此处不考虑转移字符可以为复杂表达式

    public DmLikeExpression() {
        this.op = new DmBoolOperator();
        this.op.setOperator("LIKE");
    }

    public boolean getRowFlag() {
        return this.rowFlag;
    }

    public void setRowFlag(boolean rowFlag) {
        this.rowFlag = rowFlag;
    }

    public void setRowTable(DmTableSource tab){
        this.tab = tab;
    }

    public boolean getEscapeFlag() {
        return this.escapeFlag;
    }

    public void setEscapeFlag(boolean espFlag) {
        this.escapeFlag = espFlag;
    }

    public void setEscapeChar(String esp) {
        this.escapeChar = new StringDmConst(esp);
    }

    @Override
    public void setWidt(){
        wid_t = 3;
        if(!rowFlag){
            if(expLeft != null && expRight != null){
                wid_t = 1;
                expLeft.setWidt();
                wid_t += expLeft.getWidt();
                expRight.setWidt();
                wid_t += expRight.getWidt();
            }
        }else {
            if(expRight != null){
                wid_t = 2;
                expRight.setWidt();
                wid_t += expRight.getWidt();
            }
        }
        if(escapeFlag){
            wid_t += 2;
        }
    }

    @Override
    public void setDept(){
        dep_t = 1;
        if(!rowFlag){
            if(expLeft != null && expRight != null){
                expLeft.setDept();
                dep_t = expLeft.getDept();
                expRight.setDept();
                if(expRight.getDept() > dep_t){
                    dep_t = expRight.getDept();
                }
            }
        }else{
            if(expRight != null){
                expRight.setDept();
                if(expRight.getDept() > dep_t){
                    dep_t = expRight.getDept();
                }
            }
        }
        if(escapeFlag){
            if(escapeChar != null){
                escapeChar.setDept();
                if(escapeChar.getDept() > dep_t){
                    dep_t = escapeChar.getDept();
                }
            }
        }
    }

    @Override
    public String toSql(){
        if(expRight == null){
            return " " + this.getClass().getSimpleName() + getSqlTarget();
        }else {
            StringBuilder sql = new StringBuilder();
            sql.append(" ");
            if(!rowFlag){
                sql.append(expLeft.toSql());

            }else {
                sql.append(tab.getSelectName() + ".ROW");
            }
            if(notFlag == true){
                sql.append(" NOT");
            }
            sql.append(op.toSql());
            sql.append(expRight.toSql());
            if(escapeFlag){
                sql.append(String.format(" ESCAPE %s " , escapeChar.toSql()));
            }
            return sql.toString();
        }
    }

    @Override
    public void fill(GlobalState globalState){
        if(!isCompleted){
            if(!rowFlag){
                expLeft.fill(globalState);
            }else {
                //随机填充该查询层次的表
                tab = globalState.getRanTabFromSel();
                //需要考虑如果表没有字符类型的列，那么sql会报数据类型不匹配的错误
                //此时去掉ROW LIKE,仅使用普通的LIKE
                if(tab == null || !tab.containChar()){
                    rowFlag = false;
                    setLeftExp(new DmColumn());
                    expLeft.fill(globalState);
                }
            }
            expRight.fill(globalState);
            if(escapeFlag){
                escapeChar.fill(globalState);
            }
            isCompleted = true;
        }
    }
}
